package com.kla.power.service.impl;

import com.kla.power.entity.DeviceEntity;
import com.kla.power.entity.InverterAcAmountEntity;
import com.kla.power.entity.InverterDcAmountEntity;
import com.kla.power.entity.InverterElectricEntity;
import com.kla.power.mapper.InverterElectricMapper;
import com.kla.power.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.util.ConstUtil;
import com.kla.power.util.SnowflakeIdWorker;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 逆变器发电数据 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-06-02
 */
@Service
public class InverterElectricServiceImpl extends ServiceImpl<InverterElectricMapper, InverterElectricEntity> implements IInverterElectricService {

    @Resource
    private IDeviceService iDeviceService;
    @Resource
    private InverterElectricMapper inverterElectricMapper;
    @Resource
    private IInverterDcAmountService iInverterDcAmountService;
    @Resource
    private IInverterAcAmountService iInverterAcAmountService;
    @Resource
    private ILatestInverterElectricService iLatestInverterElectricService;

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public InverterElectricEntity saveSingle (InverterElectricEntity inverterElectricEntity) {
        if ( inverterElectricEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        String systemId = SnowflakeIdWorker.getInstance ().nextStringId ();
        inverterElectricEntity.setId ( systemId );
        inverterElectricEntity.setCreateTime ( new Date () );
        //防止4G发送错误数据，这里根据制造商名称是否为"艾伏"进行筛选入库
        if ( inverterElectricEntity.getManufacturer ().equals ( ConstUtil.MANUFACTURER_NAME ) ) {
            //更新逆变器
            DeviceEntity deviceEntity = new DeviceEntity ();
            deviceEntity.setDeviceSn ( inverterElectricEntity.getSerialNumber () );
            deviceEntity = iDeviceService.querySingle ( deviceEntity );
            if ( deviceEntity == null ) {
                deviceEntity = new DeviceEntity ();
                //通过逆变器中带来的数采SN获取父数采
                deviceEntity.setDeviceSn ( inverterElectricEntity.getCollectorSn () );
                deviceEntity = iDeviceService.querySingle ( deviceEntity );
                DeviceEntity inverter = new DeviceEntity ();
                if ( deviceEntity != null ) {
                    //父数采存在
                    inverter.setParentId ( deviceEntity.getId () );
                    inverter.setStationId ( deviceEntity.getStationId () );
                }
                inverter.setDeviceType ( ConstUtil.DEVICE_TYPE_INVERTER );
                inverter.setDeviceSn ( inverterElectricEntity.getSerialNumber () );
                iDeviceService.saveSingle ( inverter );
            }
            inverterElectricEntity.setStationId ( deviceEntity.getStationId () );
            int insert = baseMapper.insert ( inverterElectricEntity );
            if ( insert == 0 ) {
                throw new StateException ( StateEnum.INSERT_EXCE );
            }
            iLatestInverterElectricService.saveOrUpdateSingle ( inverterElectricEntity );
            //处理直流
            List<InverterDcAmountEntity> inverterDcAmountEntityList = inverterElectricEntity.getInverterDcAmountEntityList ();
            for ( InverterDcAmountEntity inverterDcAmountEntity : inverterDcAmountEntityList ) {
                inverterDcAmountEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                inverterDcAmountEntity.setInverterSn ( inverterElectricEntity.getSerialNumber () );
                inverterDcAmountEntity.setInverterSystemId ( systemId );
                inverterDcAmountEntity.setCreateTime ( new Date () );
                boolean save = iInverterDcAmountService.save ( inverterDcAmountEntity );
                if ( ! save ) {
                    throw new StateException ( StateEnum.INSERT_EXCE );
                }
            }
            //处理交流
            List<InverterAcAmountEntity> inverterAcAmountEntityList = inverterElectricEntity.getInverterAcAmountEntityList ();
            for ( InverterAcAmountEntity inverterAcAmountEntity : inverterAcAmountEntityList ) {
                inverterAcAmountEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
                inverterAcAmountEntity.setInverterSn ( inverterElectricEntity.getSerialNumber () );
                inverterAcAmountEntity.setInverterSystemId ( systemId );
                inverterAcAmountEntity.setCreateTime ( new Date () );
                boolean save = iInverterAcAmountService.save ( inverterAcAmountEntity );
                if ( ! save ) {
                    throw new StateException ( StateEnum.INSERT_EXCE );
                }
            }
        }
        return inverterElectricEntity;
    }

    @Override
    public InverterElectricEntity querySingle (InverterElectricEntity inverterElectricEntity) {
        InverterElectricEntity inverter = inverterElectricMapper.querySingle ( inverterElectricEntity );
        return inverter;
    }

    @Override
    public InverterElectricEntity queryMaxData (InverterElectricEntity inverterSystemEntity) {
        if ( inverterSystemEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        InverterElectricEntity inverterSystem = inverterElectricMapper.queryMaxDate ( inverterSystemEntity );
        return inverterSystem;
    }


    /**
     * 查询一台逆变器从startTime到endTime时间内的发电数据
     * @param inverterSystemEntity
     * @return
     */
    @Override
    public List<InverterElectricEntity> queryHistoryData (InverterElectricEntity inverterSystemEntity) {
        List<InverterElectricEntity> inverterSystemEntities = inverterElectricMapper.queryHistoryData ( inverterSystemEntity );
        return inverterSystemEntities;
    }


    /**
     * 获取一台逆变器当天发电最大数据
     * @param inverterSystemEntity
     * @return
     */
    @Override
    public InverterElectricEntity queryInverterTodayMax (InverterElectricEntity inverterSystemEntity) {
        // 设置inverter_system_info分区名称
        LocalDate localDate = LocalDate.now ();
        //String yearAndMonth = localDate.getYear () + "" + ( localDate.getMonthValue () < 10 ? "0" + localDate.getMonthValue () : localDate.getMonthValue () );
        //String partitionName = "partition (p" + yearAndMonth + ")";
        //inverterSystemEntity.setPartitionName ( partitionName );
        InverterElectricEntity inverterSystem = inverterElectricMapper.queryInverterTodayMax ( inverterSystemEntity );
        return inverterSystem;
    }

}
